package com.tiancheng.trade.payment.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.web.model.request.OrderPayDTO;
import com.tiancheng.trade.commom.web.model.response.SettlementRuleResVO;
import com.tiancheng.trade.payment.bo.SettleFlowDetailMerchantBO;
import com.tiancheng.trade.payment.bo.notify.PayNotifySubBO;
import com.tiancheng.trade.payment.channel.bo.PaymentFlowDetailBO;
import com.tiancheng.trade.payment.model.PaymentFlowDetail;
import com.tiancheng.trade.payment.vo.admin.res.PaymentFlowDetailResVo;
import com.tiancheng.trade.payment.vo.api.QueryFlowDetailVO;
import com.tiancheng.trade.payment.vo.api.UnifiedOrderVO;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 支付流水明细业务接口
 *
 * @author kellen
 * @date 2024/4/29
 */
public interface PaymentFlowDetailService extends IService<PaymentFlowDetail> {

    /**
     * 保存支付流水明细
     *
     * @param flowId
     * @param paymentId
     * @param unifiedOrderVO
     * @param merchantRuleMap 清分规则
     * @return
     */
    List<PaymentFlowDetail> save(String flowId, String paymentId, OrderPayDTO unifiedOrderVO, Map<String, SettlementRuleResVO> merchantRuleMap);

    /**
     * 查询流水明细
     *
     * @param queryFlowDetailVO
     * @return
     */
    PageInfo find(QueryFlowDetailVO queryFlowDetailVO);

    /**
     * 聚合应清分商户、商品编码
     *
     * @param expectSettleDate 渠道应核销日期
     * @return
     */
    List<PaymentFlowDetail> distinctSettle(LocalDate expectSettleDate,String payChannel);
    /**
     * 聚合应清分商户、商品编码
     *
     * @param expectSettleDate 渠道应核销日期
     * @return
     */
    List<SettleFlowDetailMerchantBO> distinctSettleMerchant(LocalDate expectSettleDate, String payChannel);

    /**
     * 查询应清分子订单，每次最多查询1000条
     *
     * @param expectSettleDate 渠道应核销日期
     * @param channel          支付渠道
     * @param startId          起始Id
     * @return
     */
    List<PaymentFlowDetailBO> listSettle(LocalDate expectSettleDate, String channel, Long startId);

    /**
     * 流水明细批量更新-所更新字段值相同
     *
     * @param ids       id
     * @param colValMap 字段-值：(column, value)
     * @return
     */
    int updateSettleBatch(List<Long> ids, Map<String, Object> colValMap);

    /**
     * 流水明细批量更新-所更新字段值相同
     *
     * @param subFlowIds       子流水号集合
     * @param colValMap 字段-值：(column, value)
     * @return
     */
    int updateSettleBatchBySubFlowId(List<String> subFlowIds, Map<String, Object> colValMap);
    /**
     * 根据flow_id 查询
     *
     * @param flowId
     * @return
     */
    List<PaymentFlowDetail> findByFlowId(String flowId);

    /**
     * 根据flow_id 查询流水明细+清分信息
     *
     * @param flowId
     * @return
     */
    List<PaymentFlowDetailResVo> findSettleByFlowId(String flowId);

    /**
     * 设置订单实付金额
     *
     * @param flowId
     * @param couponAmount
     * @return
     */
    List<PayNotifySubBO> updateAmount(String flowId, Integer couponAmount);

    /**
     * 批量查询
     *
     * @param condition
     * @return
     */
    List<PaymentFlowDetail> selectList(QueryWrapper<PaymentFlowDetail> condition);

    /**
     * 根据flow_id+sub_order_id 更新enterprise_code（企业用户号）
     *
     * @param flowDetails
     * @return
     */
    int updateEnterpriseCodeByFlowIdAndSubOrderId(List<PaymentFlowDetail> flowDetails);

    /**
     * 查询要执行清分的商户信息
     *
     * @param expectSettleTimeBegin 统计开始时间
     * @param expectSettleTimeEnd   统计截止时间
     * @return
     */
    List<PaymentFlowDetail> queryCouponSettleMerchant(LocalDateTime expectSettleTimeBegin, LocalDateTime expectSettleTimeEnd);

    /**
     * 查询要执行清分的商户信息
     *
     * @param expectSettleTimeBegin 统计开始时间
     * @param expectSettleTimeBegin 统计截止时间
     * @param startId               统计开始id
     * @return
     */
    List<PaymentFlowDetailBO> queryCouponSettlePayFlowDetail(LocalDateTime expectSettleTimeBegin, LocalDateTime expectSettleTimeEnd, long startId);

    /**
     * 根据支付子流水号查询
     * @param subFLowIds
     * @return
     */
    List<PaymentFlowDetail> queryBySubFlowIds(List<String> subFLowIds);
    /**
     * 查询流水明细
     * @param subFlowIds 子流水号
     * @return
     */
    List<PaymentFlowDetailBO> queryDetailBOBySubFlowIds(@Param("subFlowIds") Set<String> subFlowIds);

}
